package com.ownload;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;

public class Httputil {
	
	public static void OnWorkDown(final String path,final OnNetWorkRespones onNetWorkRespones)  {
		 final Handler handler=new Handler();
		
		new Thread(){
			public void run() {
				boolean isworkok=false;
				try {
					URL url=new URL(path);
					HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
					openConnection.setConnectTimeout(5000);
					openConnection.connect();
					if(openConnection.getResponseCode()==200){
						InputStream is = openConnection.getInputStream();
						ByteArrayOutputStream bots=new ByteArrayOutputStream();
						byte[] b=new byte[1024];
						int len=0;
						while((len=is.read(b))!=-1){
							bots.write(b, 0, len);	
						}
						is.close();
						bots.close();
						final String respose=new String(bots.toByteArray());
						
						handler.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								onNetWorkRespones.OK(respose);
								
							}
						});
						isworkok=true;
					}
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally{
					if(!isworkok){
						handler.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								onNetWorkRespones.ERROR("下载失败");
							}
						});
					}
				}
				
			};
		}.start();
		
	}
	public interface OnNetWorkRespones{
		public void OK(String respose);
		public void ERROR(String error);
	}
	private static ExecutorService execuotrs = Executors.newFixedThreadPool(3);
	public static void OnworkBitmaDown(final String path,final OnNetWorkBitmpResponse onNetWorkBitmpResponse
			){
		final Handler handler=new Handler();
		execuotrs.execute(new Runnable() {
			boolean isworkok=false;
			@Override
			public void run() {
				// TODO Auto-generated method stub
				URL url;
				try {
					url = new URL(path);
					HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
					openConnection.setConnectTimeout(5000);
					if(openConnection.getResponseCode()==200){
						InputStream inputStream = openConnection.getInputStream();
						final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
						handler.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								onNetWorkBitmpResponse.OK(bitmap);
							}
						});
						inputStream.close();
						isworkok=true;
					}
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally{
					if(!isworkok){
						handler.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								onNetWorkBitmpResponse.ERROR("下载失败");
							}
						});
					}
				}
			}
		});
	}
	public interface OnNetWorkBitmpResponse{
		public void OK(Bitmap bitmap);
		public void ERROR(String error);
	}
}
